using System;
using System.Web.Security;
using System.Web.UI;

public partial class RoleTester : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitializeLists();
        }
    }
    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtAddRoleRole.Text))
        {
            Roles.CreateRole(txtAddRoleRole.Text);
            InitializeLists();
            ddAddUserToRoleRoles.SelectedValue = txtAddRoleRole.Text;
            ddIsUserInRole.SelectedValue = txtAddRoleRole.Text;
        }
    }
    protected void btnDelRole_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtAddRoleRole.Text))
        {
            Roles.DeleteRole(txtAddRoleRole.Text);
            InitializeLists();
        }
    }
    protected void btnAddUserToRole_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtAddUserToRoleUser.Text))
        {
            Roles.AddUserToRole(txtAddUserToRoleUser.Text, ddAddUserToRoleRoles.SelectedValue);
        }
    }
    protected void btnDelUserFromRole_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtAddUserToRoleUser.Text))
        {
            Roles.RemoveUserFromRole(txtAddUserToRoleUser.Text, ddAddUserToRoleRoles.SelectedValue);
        }
    }
    protected void btnIsUserInRole_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtIsUserInRoleUser.Text) && !string.IsNullOrEmpty(ddIsUserInRole.SelectedValue))
        {
            lblIsUserInRoleAnswer.Text = Roles.IsUserInRole(txtIsUserInRoleUser.Text, ddIsUserInRole.SelectedValue).ToString();
        }
    }
    private void InitializeLists()
    {
        string[] allRoles = Roles.GetAllRoles();
        ddAddUserToRoleRoles.DataSource = allRoles;
        ddAddUserToRoleRoles.DataBind();
        ddIsUserInRole.DataSource = allRoles;
        ddIsUserInRole.DataBind();
    }
}